package com.mxnet.chongjin.supoman.activity;

import android.app.Instrumentation;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.mxnet.chongjin.supoman.R;
import com.mxnet.chongjin.supoman.config.Api;
import com.mxnet.chongjin.supoman.utils.StringUtils;
import com.mxnet.chongjin.supoman.utils.TCPUtils;
import com.mxnet.chongjin.supoman.utils.ToastUtils;

public class SecurityCodeActivity extends AppCompatActivity implements View.OnClickListener, TextWatcher {
    private Context mContext;
    private LinearLayout mLinearLayout;
    private Button mButton, mButtonReset;
    private int sequenceNum;
    private EditText mEditText1, mEditText2, mEditText3, mEditText4, mEditText;
    private SharedPreferences mPreferences;
    private SharedPreferences.Editor mEditor;
    private String MAC;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_security_code);
        getWindow().getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        getWindow().setStatusBarColor(Color.TRANSPARENT);
        mContext = SecurityCodeActivity.this;
        mPreferences = getSharedPreferences("supoman", MODE_PRIVATE);
        mEditor = mPreferences.edit();
        MAC = mPreferences.getString("MAC", "");

        initView();
        initData();
    }

    public void initData() {

    }

    public void initView() {
        mLinearLayout = findViewById(R.id.ll_security_code);
        mLinearLayout.setOnClickListener(this);

        mButton = findViewById(R.id.btn_set_security_code);
        mButton.setOnClickListener(this);

        mButtonReset = findViewById(R.id.btn_set_security_code_resetpwd);
        mButtonReset.setOnClickListener(this);

        mEditText = findViewById(R.id.et_security_code);

        mEditText1 = findViewById(R.id.et_security_code_1);
        mEditText2 = findViewById(R.id.et_security_code_2);
        mEditText3 = findViewById(R.id.et_security_code_3);
        mEditText4 = findViewById(R.id.et_security_code_4);

        mEditText1.addTextChangedListener(this);
        mEditText2.addTextChangedListener(this);
        mEditText3.addTextChangedListener(this);
        mEditText4.addTextChangedListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_security_code:
                new Thread() {
                    public void run() {
                        try {
                            Instrumentation inst = new Instrumentation();
                            inst.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }.start();
                break;
            case R.id.btn_set_security_code_resetpwd:
                mEditor.putString("startPwd", "FFFFFFFF").commit();
                ToastUtils.showToast(mContext, "重置成功！");
                break;
            case R.id.btn_set_security_code:
                if (!TextUtils.isEmpty(mEditText1.getText()) && !TextUtils.isEmpty(mEditText2.getText()) && !TextUtils.isEmpty(mEditText3.getText()) && !TextUtils.isEmpty(mEditText4.getText())) {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            sequenceNum = mPreferences.getInt("sequenceNum", 1);
                            String seq = Integer.toHexString(sequenceNum);
                            if (seq.length() == 1) {
                                seq = "000" + seq;
                            } else if (seq.length() == 2) {
                                seq = "00" + seq;
                            } else if (seq.length() == 3) {
                                seq = "0" + seq;
                            }
                            String old_pwd = "";
                            String str = mEditText.getText().toString();
                            if (str.equals("0000")) {
                                old_pwd = "FFFFFFFF";
                            } else {
                                old_pwd = String.format("%02x", (int) (str.charAt(0) - '0')) +
                                        String.format("%02x", (int) (str.charAt(1) - '0')) +
                                        String.format("%02x", (int) (str.charAt(2) - '0')) +
                                        String.format("%02x", (int) (str.charAt(3) - '0'));
                            }
                            TCPUtils.TCP_sendMsg(Api.SERVER_IP, Api.SERVER_PORT,
                                    StringUtils.hexStringToBytes(Api.HEAD + MAC+ Api.MY_MAC + old_pwd + seq +
                                            Api.PASSWORD +
                                            String.format("%02x", Integer.parseInt(mEditText1.getText().toString())) +
                                            String.format("%02x", Integer.parseInt(mEditText2.getText().toString())) +
                                            String.format("%02x", Integer.parseInt(mEditText3.getText().toString())) +
                                            String.format("%02x", Integer.parseInt(mEditText4.getText().toString()))));
                            if ((sequenceNum + 1) > 65534) {
                                sequenceNum = 0;
                                mEditor.putInt("sequenceNum", sequenceNum);
                            } else {
                                mEditor.putInt("sequenceNum", sequenceNum + 1);
                            }
                            mEditor.putString("startPwd", String.format("%02x", Integer.parseInt(mEditText1.getText().toString())) +
                                    String.format("%02x", Integer.parseInt(mEditText2.getText().toString())) +
                                    String.format("%02x", Integer.parseInt(mEditText3.getText().toString())) +
                                    String.format("%02x", Integer.parseInt(mEditText4.getText().toString())));
                            mEditor.commit();
                            ToastUtils.showToast(mContext, getResources().getString(R.string.save_success));
                            new Thread() {
                                public void run() {
                                    try {
                                        Instrumentation inst = new Instrumentation();
                                        inst.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                }
                            }.start();
                        }
                    }).start();
                } else {
                    ToastUtils.showToast(mContext, "请填写控制密码！");
                }
                break;
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        if (!TextUtils.isEmpty(mEditText1.getText())) {
            if (!TextUtils.isEmpty(mEditText2.getText())) {
                if (!TextUtils.isEmpty(mEditText3.getText())) {
                    if (!TextUtils.isEmpty(mEditText4.getText())) {
                        mEditText4.clearFocus();
                    } else {
                        mEditText1.clearFocus();
                        mEditText2.clearFocus();
                        mEditText3.clearFocus();
                        mEditText4.requestFocus();
                    }
                } else {
                    mEditText1.clearFocus();
                    mEditText2.clearFocus();
                    mEditText3.requestFocus();
                    mEditText4.clearFocus();
                }
            } else {
                mEditText1.clearFocus();
                mEditText2.requestFocus();
                mEditText3.clearFocus();
                mEditText4.clearFocus();
            }
        } else {
            mEditText1.requestFocus();
            mEditText2.clearFocus();
            mEditText3.clearFocus();
            mEditText4.clearFocus();
        }
    }
}
